<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>体育项目管理维护界面</title>
    <link rel="stylesheet"
          href="../../res/zTree_v3-master/css/metroStyle/metroGreen.css"
          type="text/css">
    <script src="../../res/js/base.js"></script>
    <script src="../../res/js/jquery-3.4.1.min.js"></script>
    <script src="../../res/layui/layui.js"></script>
    <script src="../../res/js/Tools.js"></script>
    <script type="text/javascript" src="../../res/module/common.js"></script>
    <script type="text/javascript" src="../../res/module/treeSelect/treeSelect.js"></script>
    <link rel="stylesheet" href="../../res/layui/css/layui.css">


    <style type="text/css">
        #main {
            margin: 10px;
        }

        .layui-input-block{
            width:60%;
            margin-left: 148px;

        }

        .layui-form-label{
            width:100px;
            text-align: center;
        }


        body{
            padding-top:5px;
        }
    </style>


</head>
<body>



<form class="layui-form" action="" id="sportForm">
    <input type="hidden" name="sportId" id="sportId"/>

    <div class="layui-form-item">
        <label class="layui-form-label">项目名称</label>
        <div class="layui-input-block">
            <input type="text" name="sportName" id="sportName" required  lay-verify="required" placeholder="请输入项目名称" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">

        <label class="layui-form-label">选择指标</label>


        <div class="layui-input-block" id="treeDiv">
            <input name="atsBaseItem" type="text" id="tree" placeholder="选择指标"
                   lay-filter="tree" class="layui-input">
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">项目说明</label>
        <div class="layui-input-block">
            <input type="text" name="sportExp" id="sportExp" required  lay-verify="required" placeholder="请输入项目说明" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div id="bscjDiv">
        <div id="tableDiv">
            <table id="demo" lay-filter="test"></table>
        </div>
    </div>


    <div class="layui-form-item" id="buttonDiv">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script>
    var treeSelect = null;
    var layer = null;
    layui.use(['treeSelect','layer'], function () {
        var setting = {
            view: {
                selectedMulti: true
            },
            check: {
                enable: true
            },
            data: {
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "pId",
                    rootPId: null
                }
            },
        };
        layer = layui.layer;


        treeSelect = layui.treeSelect;
        if (getQueryStringOne("sportId") != '') {
            var index1 = layer.load(2);
            findById(treeSelect, setting,index1);
        } else {
            tree(treeSelect, setting);
        }
    });
    function tree(treeSelect, setting) {
        treeSelect.render({
            // 选择器
            elem: '#tree',

            // 数据
            data: getRootPath()+"AtsBaseSportController/selectXiaLa",
            // 异步加载方式：get/post，默认get
            type: 'post',
            // 占位符
            placeholder: '',
            // 是否开启搜索功能：true/false，默认false
            search: true,
            setting: setting,
            // 点击回调
            click: function (d) {
                console.log(d.current.id);
                document.getElementById("tree").value = d.current.id;
            },
            check: function (d) {
                var value = "";
                for (var i = 0; i < d.current.length; i++) {
                    value += d.current[i].id;

                    if (i < d.current.length - 1) {
                        value += ","
                    }
                }


                document.getElementById("tree").value = value;

            },
            // 加载完成后的回调函数
            success: function (d) {

            }

        });
    }
</script>
<script>

    var form = null ;
    layui.use('form', function(){
        form = layui.form;
        var laydate = layui.laydate;
        //监听提交
        form.on('submit(formDemo)', function(data){
            var index2 = layer.load(0);
            //layer.msg(JSON.stringify(data.field));
            var formData = new FormData(document.getElementById("sportForm"));

            var url = getRootPath()+'AtsBaseSportController/saveOrUpdate';

            $.ajax({
                url:url,
                type:"post",
                data:formData,
                processData:false,
                contentType:false,
                dataType:"json",
                success:function(obj){
                    layer.close(index2);
                    if(obj.num==1){

                        layer.msg(obj.msg, {
                            icon: 1,
                            time: 1000 //1秒关闭（如果不配置，默认是3秒）
                        }, function(){
                            //关闭当前层
                            //当你在iframe页面关闭自身时
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                            //刷新父页面
                            parent.location.reload();
                        });
                    }
                },
                error:function(e){
                    layer.msg("操作错误!");
                }
            });
            return false;
        });
        findById();//修改页面的赋值
    });

    //修改页面的赋值
    function findById(treeSelect, setting,index1){

        var sportId = getQueryString("sportId");
        if(!sportId){
            return ;
        }
        console.info(sportId);
        var url=getRootPath()+'AtsBaseSportController/selectByPrimaryKey'

        var data = {sportId:sportId};
        $.post(url,data,function(obj){
            layer.close(index1);
            $("#sportId").val(obj.ats.sportId);
            $("#sportName").val(obj.ats.sportName);
            $("#sportExp").val(obj.ats.sportExp);
            $("#tree").val(obj.one);

            $("#tresDiv").css('display',"none");
            tree(treeSelect, setting);
            +
                //重新渲染
                form.render();
        },"json");
    }
    //得到地址栏上的参数  可以 是中文
    function getQueryStringOne(key) {0

        var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null)
            return decodeURI(r[2]);
        return null;
    }


</script>
</body>
</html>